/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Implementation.SourceCode.entities;

import Implementation.SourceCode.Utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author mrapple
 */
@Entity
@Table(name = "WAREHOUSE")
public class WareHouse {

    private String name;
    private int id;

    @Id
    @GeneratedValue
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void save() {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.save(this);
            tx.commit();
            session.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Transient
    public List<WareHouse> getAllWareHouse() {
        List<WareHouse> lstWareHouse = new ArrayList<WareHouse>();
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            lstWareHouse = session.createQuery("from WareHouse").list();
            tx.commit();
            session.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return lstWareHouse;
    }
    
    @Override
    public String toString(){
        return name;
    }
}
